var Config = require('./config.js');
// let BASEURL = 'http://health.cpzsyun.com/health/'
let BASEURL = '/api/'
export const request = (options = {}) => {
	return new Promise((resolve, rejects) => {
		handleRequest(options, resolve, rejects)
	})
}
// 发起请求
function handleRequest(options, resolve, reject) {
	const token = uni.getStorageSync('token')||""
	// uni.showLoading({
	// 	title: ""
	// });
	uni.request({
		url: BASEURL + options.url,
		method: options.method,
		header: {
			token,
		},
		timeout:40000,
		data: options.data,
		success: (response) => {
			// uni.hideLoading();
			if(response.data.code==108){
				uni.showToast({
				  icon: 'none',
				  position: 'top',
				  title: '请登录！'
				});
				return
			}
			if(response.data.code==105){
				uni.showToast({
				  icon: 'none',
				  position: 'top',
				  title: '登录超时,重新登录！'
				});
				if(JSON.parse(uni.getStorageSync('doctorInfo')).type=='US'){
						setTimeout(()=>{
							uni.clearStorageSync();
							uni.switchTab({
								url:'/pages/mine/mine'
							})
						},1500)
						uni.clearStorageSync()
					}else{
						setTimeout(()=>{
							uni.clearStorageSync();
							uni.navigateTo({
								url:'/pages/login/login'
							})
						},1500)
						uni.clearStorageSync()
					}
				return 
			}
			if(response.statusCode!=200&&response.statusCode!=403){
				uni.showToast({
				  icon: 'none',
				  position: 'top',
				  title: '出错了！'
				});
				return 
			}
			return resolve(response.data)
		},
		fail: (err) => {
			uni.showToast({
			  icon: 'error',
			  position: 'top',
			  title: '网络错误！'
			});
			// uni.hideLoading();
			return 
		}
	})
}